package org.bing.engine.console.service;

import java.util.ArrayList;
import java.util.List;

import org.bing.engine.core.domain.ApplicationOfDomain;
import org.bing.engine.core.domain.ControllerGroup;
import org.bing.engine.core.domain.ControllerGroupOfDomain;
import org.bing.engine.core.domain.Domain;

public class ControllerGroupOfDomainManager extends AbstractHibernateManager {

    @SuppressWarnings("unchecked")
    public List<ControllerGroup> findControllerGroupByDomain(Domain domain) {
        if (domain == null) {
            return new ArrayList<ControllerGroup>();
        }
        return template.find("select t.group from ControllerGroupOfDomain t where t.domain=?", domain);
    }

    public void save(ControllerGroupOfDomain grpOfDomain) {
        template.save(grpOfDomain);
    }

    public void delete(ControllerGroupOfDomain grpOfDomain) {
        if (grpOfDomain.getId() > 0) {
            template.delete(grpOfDomain);
        } else {
            String hql = "select t from ApplicationOfDomain t where t.group=? and t.domain=?";
            Object[] args = new Object[] { grpOfDomain.getGroup(), grpOfDomain.getDomain() };
            ApplicationOfDomain grpOfDomain2 = (ApplicationOfDomain) super.findUniqueObject(hql, args);
            if (grpOfDomain2 != null) {
                template.delete(grpOfDomain2);
            } else {
                logger.warn("Can't find for " + grpOfDomain + ", not delete.");
            }
        }
    }
}
